import axios from './axios.js';

export default {
    namespaced:true,
    state: {
        username: ''
    },
    actions:{
        /**
         * 登录接口
         * @param commit
         * @param state
         * @param params 请求参数
         * @return {Promise<*[]>}
         */
        async loginApi({commit, state}, params) {
            try {
                let {data} = await axios.post('/api/account/login/', params);
				localStorage.setItem('token', data.access_token);
				sessionStorage.setItem('token',data.access_token);
				plus.storage.setItem("token",data.access_token);
                commit('setToken', data, {root: true});
                return [null, data]
            } catch (e) {
                return [e, null]
            }
        },
		/**
		 * APP微信绑定
		 * @param commit
		 * @param state
		 * @param params 请求参数
		 * @return {Promise<*[]>}
		 */
		async bindingWeixinApi({commit, state}, params) {
		    try {
		        let {data} = await axios.post('/api/account/wlogin/binding/one', params);
		        localStorage.setItem('token', data.access_token);
		        sessionStorage.setItem('token',data.access_token);
		        plus.storage.setItem("token",data.access_token);
				commit('setToken', data, {root: true});
				return [null, data]
		    } catch (e) {
		        return [e, null]
		    }
		},
        /**
         * 注册
         * @param commit
         * @param state
         * @param params 请求参数
         * @return {Promise<*[]>}
         */
        async getRegisterApi({commit, state}, params) {
            try {
                let {data} = await axios.post('/api/account/login/register', params);
                return [null, data]
            } catch (e) {
                return [e, null]
            }
        },
		/**
         * 获取二维码id
         * @param commit
         * @param state
         * @param params 请求参数
         * @return {Promise<*[]>}
         */
        async getCodeApi({commit, state}, params) {
            try {
                let {data} = await axios.get('/api/account/login/code', {params});
				console.log(data);
                return [null, data]
            } catch (e) {
                return [e, null]
            }
        },
        /**
         * 忘记密码1 输入手机发送验证码
         * @param commit
         * @param state
         * @param params 请求参数
         * @return {Promise<*[]>}
         */
        async forgetVcodeApi({commit, state}, params) {
            try {
                let {data} = await axios.get('/api/account/login/forget_vcode', {params});
                return [null, data]
            } catch (e) {
                return [e, null]
            }
        },
		/**
         * 忘记密码2 实时验证验证码
         * @param commit
         * @param state
         * @param params 请求参数
         * @return {Promise<*[]>}
         */
        async forgetInspectApi({commit, state}, params) {
            try {
                let {data} = await axios.get('/api/account/login/forget_inspect', {params});
                return [null, data]
            } catch (e) {
                return [e, null]
            }
        }, 
		/**
         * 获取登录二维码(请先用id链接websocket)
         * @param commit
         * @param state
         * @param params 请求参数
         * @return {Promise<*[]>}
         */
        async loginQrApi({commit, state}, params) {
            try {
                let {data} = await axios.get('/api/account/login/qr', {params});
                return [null, data]
            } catch (e) {
                return [e, null]
            }
        }, 
		/**
         * 忘记密码3 
         * @param commit
         * @param state
         * @param params 请求参数
         * @return {Promise<*[]>}
         */
        async forgetUpApi({commit, state}, params) {
            try {
                let {data} = await axios.post('/api/account/login/forget_up', params);
                return [null, data]
            } catch (e) {
                return [e, null]
            }
        },
		/**
         * 获取验证码
         * @param commit
         * @param state
         * @param params 请求参数
         * @return {Promise<*[]>}
         */
        async loginVcodeApi({commit, state}, params) {
            try {
                let {data} = await axios.get('/api/account/login/login_vcode', {params});
                return [null, data]
            } catch (e) {
                return [e, null]
            }
        },
		/**
         * 获取验证码
         * @param commit
         * @param state
         * @param params 请求参数
         * @return {Promise<*[]>}
         */
        async phoneCodeApi({commit, state}, params) {
            try {
                let {data} = await axios.get(`/api/account/basics/phone_code/${params.type}/${params.phone}`);
                return [null, data]
            } catch (e) {
                return [e, null]
            }
        },
		/**
		 * 校验验证码
		 * @param commit
		 * @param state
		 * @param params 请求参数
		 * @return {Promise<*[]>}
		 */
		async checkCodeApi({commit, state}, params) {
		    try {
		        let {data} = await axios.get('/api/account/basics/check_code',{params});
		        return [null, data]
		    } catch (e) {
		        return [e, null]
		    }
		},
		/**
         * 查询用户基本信息
         * @param commit
         * @param state
         * @param params 请求参数
         * @return {Promise<*[]>}
         */
		async userInfoApi({commit, state}, params) {
		    try {
		        let {data} = await axios.get('/api/account/user/info', {params});
				commit('setUser', data, {root: true});
		        return [null, data]
		    } catch (e) {
		        return [e, null]
		    }
		},
		/**
         * 文件上传
         * @param commit
         * @param state
         * @param params 请求参数
         * @return {Promise<*[]>}
         */
		async putUploadApi({commit, state}, params) {
		    try {
		        let {data} = await axios.put('/api/account/upload', params);
		        return [null, data]
		    } catch (e) {
		        return [e, null]
		    }
		},
		/**
		 * 文件删除
		 * @param commit
		 * @param state
		 * @param params 请求参数
		 * @return {Promise<*[]>}
		 */
		async delUploadApi({commit, state}, params) {
		    try {
		        let {data} = await axios.delete('/api/account/upload', {params});
		        return [null, data]
		    } catch (e) {
		        return [e, null]
		    }
		},
		/**
		 * 查询相关协议
		 * @param commit
		 * @param state
		 * @param params 请求参数
		 * @return {Promise<*[]>}
		 */
		async getSetupApi({commit, state}, params) {
		    try {
		        let {data} = await axios.get('/api/account/setup/get', {params});
		        return [null, data]
		    } catch (e) {
		        return [e, null]
		    }
		},
		/**
		 * 最新版本号
		 * @param commit
		 * @param state
		 * @param params 请求参数
		 * @return {Promise<*[]>}
		 */
		async lastVersionApi({commit, state}, params) {
		    try {
		        let {data} = await axios.get('/api/account/version/last', {params});
		        return [null, data]
		    } catch (e) {
		        return [e, null]
		    }
		},
		/**
		 * 获取联系方式
		 * @param commit
		 * @param state
		 * @param params 请求参数
		 * @return {Promise<*[]>}
		 */
		async basicsSetupsApi({commit, state}, params) {
		    try {
		        let {data} = await axios.get('/api/account/basics/setup', {params});
		        return [null, data]
		    } catch (e) {
		        return [e, null]
		    }
		},
		/**
		 * APP微信授权登录
		 * @param commit
		 * @param state
		 * @param params 请求参数
		 * @return {Promise<*[]>}
		 */
		async appWloginApi({commit, state}, params) {
		    try {
		        let {data} = await axios.post('/api/account/wlogin/app', params);
		        return [null, data]
		    } catch (e) {
		        return [e, null]
		    }
		},
		
		/**
		 * APP微信授权登录
		 * @param commit
		 * @param state
		 * @param params 请求参数
		 * @return {Promise<*[]>}
		 */
		async basicsSetupApi({commit, state}, params) {
		    try {
		        let {data} = await axios.get('/api/account/wlogin/unbundling', {params});
		        return [null, data]
		    } catch (e) {
		        return [e, null]
		    }
		},
    },
    mutations:{
        /**
         * 设置用户名
         * @param state
         * @param data
         */
        setUsername(state, data){
        	console.log(data)
            state.username = data;
        }
    }
}
